<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\PerformanceLevel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class PerformanceLevelController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PerformanceLevel(), function (Grid $grid) {
            $grid->model()->orderBy('new_order_rate', 'asc');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('min_amount');
            $grid->column('max_amount');
            $grid->column('new_order_rate','新零售奖励比例');
            $grid->column('new_group_rate','团购奖励比例');
            $grid->column('peer_rates', '平级奖比例')->display(function ($peer_rates) {
                return sprintf('平一级：%.2f 平二级：%.2f', $peer_rates[1], $peer_rates[2]);
            });
            $grid->column('participate_dividend', '参加分红下单数');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PerformanceLevel(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('min_amount');
            $show->field('max_amount');
            $show->field('new_order_rate');
            $show->field('peer_rates');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PerformanceLevel(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('min_amount');
            $form->text('max_amount');
            $form->text('new_order_rate','新零售奖励比例');
            $form->text('new_group_rate','团购奖励比例');
            $form->keyValue('peer_rates', '平级奖')->default(['1' => '0.05', '2' => '0.02'])
                ->setKeyLabel('平级奖')
                ->setValueLabel('百分比');
            $form->text('participate_dividend', '参加分红下单数');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
